<?php
namespace Common\Common;


class Menu
{
    public $root = null;
    protected $data = null;
    public $parent = null;
    public $child = Array();

    public function __construct($data)
    {
        $this->data = $data;
        if ($this->get_parent_id() == 0) {
            $this->set_root($this);
        }
    }

    public function set_parent($parent)
    {
        $this->parent = $parent;
        $this->set_root($parent->get_root());
        $parent->set_child($this);
    }

    public function set_child($child)
    {
        $this->child[] = $child;
    }

    public function get_child()
    {
        return $this->child;
    }

    public function set_root($root)
    {
        $this->root = $root;
    }

    public function get_id()
    {
        return $this->data['id'];
    }

    public function get_parent_id()
    {
        if (empty($this->parent)) {
            $parent_data = $this->parent - $this->get_data();
            if (empty($parent_data)) {
                return $parent_data['id'];
            }
        }
        return 0;
    }

    public function get_data($key)
    {
        return $this->data[$key];
    }

    public function get_root()
    {
        return $this->root;
    }
}